package cn.learn.activiti.service.impl;

import cn.learn.activiti.service.IHistoricTaskService;
import lombok.RequiredArgsConstructor;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricVariableInstance;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author huangyezhan
 * @version 1.0
 * @date 2021年03月11日20:06
 */
@Service
@RequiredArgsConstructor
public class HistoricTaskServiceImpl implements IHistoricTaskService {

    private final HistoryService historyService;

    @Override
    public List<HistoricTaskInstance> historicTaskInstanceByAssignee(int firstResult, int maxResults, String authorization) {
        return historyService.createHistoricTaskInstanceQuery()
                .taskAssignee(authorization)
                .orderByHistoricTaskInstanceEndTime().asc()
                .listPage(firstResult, maxResults)
                ;
    }

    public void historicPro(int firstResult, int maxResults) {
        historyService.createNativeHistoricProcessInstanceQuery()
                .sql("select * from act_hi_procinst where END_ACT_ID_ is not null")
                .listPage(firstResult, maxResults)
        ;
    }

    public void finishedProcess(String processInstanceId) {
        //查看流程历史完成没有
        HistoricProcessInstance instance = historyService.createHistoricProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .finished()
                .singleResult();

        //该流程已经完成，获取该流程所有的任务
        if (null != instance) {
            List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
                    .processInstanceId(processInstanceId)
                    .list();
        }
    }

    @Override
    public List<HistoricProcessInstance> initiatedTask(String authorization) {
        return historyService.createHistoricProcessInstanceQuery()
                .startedBy(authorization)
                .orderByProcessInstanceStartTime()
                .asc()
                .list();
    }

    @Override
    public HistoricProcessInstance getProcessInstance(String processInstanceId) {
        return historyService.createHistoricProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .singleResult();
    }


    @Override
    public List<HistoricActivityInstance> getHistoricActivityInstances(String processInstanceId) {
        return historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(processInstanceId)
                .orderByHistoricActivityInstanceStartTime()
                .desc()
                .list();
    }

    @Override
    public List<HistoricVariableInstance> getHistoryVariablesByTaskId(String taskId) {
        return historyService.createHistoricVariableInstanceQuery()
                .taskId(taskId)
                .list();
    }

    @Override
    public HistoricTaskInstance getHistoryTaskById(String taskId) {
        return historyService.createHistoricTaskInstanceQuery()
                .taskId(taskId)
                .singleResult();
    }

    @Override
    public List<HistoricTaskInstance> getHistoryTaskByBusinessKey(String businessKey) {
        return historyService.createHistoricTaskInstanceQuery()
                .processInstanceBusinessKey(businessKey)
                .orderByTaskCreateTime()
                .desc()
                .list();
    }


}
